/* eslint-disable complexity */
/* eslint-disable no-console */
/* eslint-disable no-shadow */
/* eslint-disable no-catch-shadow */
let extensionMap = {
	$callBridgeHandler({
		name,
		nameMessage = '',
		data = {},
		errorCallback = ex => console.error(ex)
	} = {}) {
		if (!name) {
			return errorCallback(new Error('未指定与客户端交互方法'));
		}
		try {
			window.WebViewJavascriptBridge.callHandler(name, {
				'code': 1,
				'name': nameMessage,
				data
			}, data.callback || null);
		} catch(ex) {
			try {
				if (data.callback) {
					let callbackName = `callback_${data.callback.name}_${new Date().getTime()}`;
					window[callbackName] = data.callback;
					data.callback = callbackName;
				}
				window.AppBridge[name](...Object.values(data));
			} catch(ex) {
				errorCallback(ex);
			}
		}
	}
};

export default {
	install(Vue, options) {
		Object.entries(extensionMap).forEach(([key, val]) => Vue[key] = Vue.prototype[key] = val);
	}
};
